c[27][27];d[999];a[200];
s;l;w;
f(int*a,int*b){return *a-*b;}
main(i,j,x,y,n,k)
{
		for(scanf("%d",&n),j=1;j<=n;j++)
			for(i=1;i<=n;i++)
				scanf("%d",&c[i][j]);
	s=0;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		{
			if(c[i][j]){d[l*2]=i;d[1+2*l++]=j;}
			while(l)
			{
				k=--l;
				x=d[2*k];
				y=d[2*k+1];
				s++;
				c[x][y]=0;
				if(c[x][y+1]){d[k*2]=x;d[k++*2+1]=y+1;c[x][y+1]=0;}
				if(c[x][y-1]){d[k*2]=x;d[k++*2+1]=y-1;c[x][y-1]=0;}
				if(c[x+1][y]){d[k*2]=x+1;d[k++*2+1]=y;c[x+1][y]=0;}
				if(c[x-1][y]){d[k*2]=x-1;d[k++*2+1]=y;c[x-1][y]=0;}
				l=k;
			}
			if(s)a[w++]=s;
			s=0;
		}

	printf("%d\n",w);
	qsort(a,w,4,f);
	for(i=0;i<w;)
		printf("%d\n",a[i++]);
}
